package com.example.music.controller;

import com.alibaba.fastjson2.JSON;
import com.example.music.domain.IogInformation;
import com.example.music.service.IIogInformationService;
import com.example.music.service.IUserService;
import com.example.music.util.AjaxResult;
import com.example.music.util.Anonymous;
import com.example.music.util.RedisCache;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Api(tags = "RedisIogInformation订单物流信息测验(224 魏川子)")
@Anonymous
@RestController
@RequestMapping("/api/IogInformation/redis")
public class RedisIogInformationController {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private IIogInformationService iogInformationService;

    @ApiOperation("通过key和Value取得redis设定的数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "key", dataType = "String",dataTypeClass = String.class),
            @ApiImplicitParam(name = "value", value = "value", dataType = "String",dataTypeClass = String.class)
    })
    @GetMapping("/put")
    public AjaxResult put(String key, String value){
        redisCache.setCacheObject(key,value,1, TimeUnit.HOURS);
        String v = redisCache.getCacheObject(key);
        Map<String,Object> result = new HashMap<>();
        result.put("key",key);
        result.put("value",v);
        return AjaxResult.success(result);
    }

    @ApiOperation("传入id,查询物流信息,将物流信息放入缓存")
    @ApiImplicitParam(name = "id",value ="用户ID",required = true,dataType = "long",paramType = "query",dataTypeClass = Long.class)
    @GetMapping("putObj")
    public AjaxResult putObj(@RequestParam Long id){
        IogInformation iogInformation = iogInformationService.selectIogInformationById(id);
        String jsonStr = JSON.toJSONString(iogInformation);
        redisCache.setCacheObject(String.valueOf(iogInformation.getId()),jsonStr,1,TimeUnit.HOURS);
        return AjaxResult.success(iogInformation);
    }

    @ApiOperation("传入id,从缓存中获取物流信息")
    @ApiImplicitParam(name = "id",value = "用户ID",required = true,dataType = "long",paramType = "query",dataTypeClass = Long.class)
    @GetMapping("/getObj")
    public AjaxResult getObj(@RequestParam Long id){
        String v = redisCache.getCacheObject(String.valueOf(id));
        System.out.println(v);
        IogInformation iogInformation = JSON.parseObject(v,IogInformation.class);
        return AjaxResult.success(iogInformation);
    }
}
